unit DCFStringGrid;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids;

type
  {forward declarations}
  TDCFStringGrid = class;
  TDCFSGColumn = class;

  TDCFSGHeaderClickEvent = procedure( Sender: TDCFSGColumn ) of object;
  TDCFSGHeaderStyle = (sgsDefault, sgsButton);
  TDCFSGColumnHeader = class(TPersistent)
  private
    FStyle   : TDCFSGHeaderStyle;
    FCaption : TCaption;
    FFont    : TFont;
  public
    constructor Create;
    destructor Destroy; override;
  published
    property Caption: TCaption read FCaption write FCaption;
    property Style: TDCFSGHeaderStyle read FStyle write FStyle default sgsDefault;
    property Font: TFont read FFont;
  end;

  TDCFSGColumn = class(TCollectionItem)
  private
    FAlignment : TAlignment;
    FWidth     : integer;
    FHeader    : TDCFSGColumnHeader;
    FColor     : TColor;
    FFont      : TFont;
    FOnClick   : TDCFSGHeaderClickEvent;
  public
    constructor Create(Collection: TCollection); override;
    destructor Destroy; override;
  published
    property Header: TDCFSGColumnHeader read FHeader;
    property Color: TColor read FColor write FColor default clWindow;
    property OnClick: TDCFSGHeaderClickEvent read FOnClick write FOnClick;
    property Font: TFont read FFont;
    property Alignment: TAlignment read FAlignment write FAlignment;
    property Width: integer read FWidth write FWidth;
  end;

  TDCFSGColumnCollection = class(TCollection)
  private
    FOwner : TDCFStringGrid;
  protected
    function GetOwner: TPersistent; override;
  public
    constructor Create( AOwner: TDCFStringGrid );
  end;

  TCreateEditorEvent = procedure( Sender: TDCFStringGrid; var Editor: TInplaceEdit ) of object;
  TBeginEditEvent = procedure( Sender: TDCFStringGrid ) of object;
  TEndEditEvent = procedure( Sender: TDCFStringGrid ) of object;
  TDCFStringGrid = class(TStringGrid)
  private
    { Private declarations }
    FColumns : TDCFSGColumnCollection;
    FOnCreateEditor : TCreateEditorEvent;
    FOnBeginEdit: TBeginEditEvent;
    FOnEndEdit: TEndEditEvent;
    procedure EnterEditor( Sender: TObject );
    procedure ExitEditor( Sender: TObject );
  protected
    { Protected declarations }
    function CreateEditor: TInplaceEdit; override;
  public
    { Public declarations }
    constructor Create( AOwner: TComponent ); override;
    destructor Destroy; override;
  published
    { Published declarations }
    property Columns: TDCFSGColumnCollection read FColumns;
    property OnCreateEditor: TCreateEditorEvent read FOnCreateEditor write FOnCreateEditor;
    property OnBeginEdit: TBeginEditEvent read FOnBeginEdit write FOnBeginEdit;
    property OnEndEdit: TEndEditEvent read FOnEndEdit write FOnEndEdit;
  end;

procedure Register;

implementation

type
  TInplaceEditAux = class(TInplaceEdit)
  end;

procedure Register;
begin
  RegisterComponents('DCF', [TDCFStringGrid]);
end;

{ TDCFStringGrid }

constructor TDCFStringGrid.Create(AOwner: TComponent);
begin
  inherited;
  FColumns := TDCFSGColumnCollection.Create(Self);
end;

function TDCFStringGrid.CreateEditor: TInplaceEdit;
begin
  result := nil;
  if assigned(FOnCreateEditor) then FOnCreateEditor(Self,result);
  if result=nil then result := inherited CreateEditor;
  TInplaceEditAux(result).OnEnter := EnterEditor;
  TInplaceEditAux(result).OnExit := ExitEditor;
end;

destructor TDCFStringGrid.Destroy;
begin
  FColumns.Free;
  inherited;
end;

procedure TDCFStringGrid.EnterEditor(Sender: TObject);
begin
  if assigned(FOnBeginEdit) then FOnBeginEdit(Self);
end;

procedure TDCFStringGrid.ExitEditor(Sender: TObject);
begin
  if assigned(FOnEndEdit) then FOnEndEdit(Self);
end;

{ TDCFSGColumnCollection }

constructor TDCFSGColumnCollection.Create(AOwner: TDCFStringGrid);
begin
  inherited Create(TDCFSGColumn);
  FOwner := AOwner;
end;

function TDCFSGColumnCollection.GetOwner: TPersistent;
begin
  result := FOwner;
end;

{ TDCFSGColumn }

constructor TDCFSGColumn.Create(Collection: TCollection);
begin
  inherited;
  FHeader := TDCFSGColumnHeader.Create;
  FFont := TFont.Create;
  FColor := clWindow;
end;

destructor TDCFSGColumn.Destroy;
begin
  FFont.Free;
  FHeader.Free;
  inherited;
end;

{ TDCFSGColumnHeader }

constructor TDCFSGColumnHeader.Create;
begin
  inherited;
  FFont := TFont.Create;
end;

destructor TDCFSGColumnHeader.Destroy;
begin
  FFont.Free;
  inherited;
end;

end.
